package com.k12.admim.config.satoken;

import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.jwt.StpLogicJwtForSimple;
import cn.dev33.satoken.util.SaFoxUtil;

public class CustomStpLogicJwtForSimple extends StpLogicJwtForSimple {

    @Override
    public String getTokenValueNotCut() {
        // 自定义获取Token的逻辑
        String tokenValue = SaHolder.getRequest().getHeader("Authorization");
        if (tokenValue != null && tokenValue.startsWith("Bearer ")) {
            return tokenValue;
        }
        SaStorage storage = SaHolder.getStorage();
        SaRequest request = SaHolder.getRequest();
        SaTokenConfig config = this.getConfigOrGlobal();
        String keyTokenName = this.getTokenName();
        if (storage.get(this.splicingKeyJustCreatedSave()) != null) {
            tokenValue = String.valueOf(storage.get(this.splicingKeyJustCreatedSave()));
        }

        if (SaFoxUtil.isEmpty(tokenValue) && config.getIsReadBody()) {
            tokenValue = request.getParam(keyTokenName);
        }

        if (SaFoxUtil.isEmpty(tokenValue) && config.getIsReadHeader()) {
            tokenValue = request.getHeader(keyTokenName);
        }

        if (SaFoxUtil.isEmpty(tokenValue) && config.getIsReadCookie()) {
            tokenValue = request.getCookieValue(keyTokenName);
            if (SaFoxUtil.isNotEmpty(tokenValue) && config.getCookieAutoFillPrefix()) {
                tokenValue = config.getTokenPrefix() + " " + tokenValue;
            }
        }
        return tokenValue;
    }

}
